<!--
 * @Author       : xiaowu wwl093000@163.com
 * @Date         : 2023-05-23 09:04:50
 * @LastEditTime : 2023-05-31 10:00:54
 * @LastEditors  : xiaowu wwl093000@163.com
 * @FilePath     : /erp-admin-ui/src/views/distribution/record/index.vue
 * @Description  : -
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="86px">
      <el-form-item label="请选择店铺" prop="storeId">
        <el-select v-model="queryParams.storeId" class="width4" placeholder="请选择" clearable @change="storeChange">
          <el-option v-for="dict in storeList" :key="dict.value" :label="dict.storeUserName +' - '+ dict.lable" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="服务类型" prop="type">
        <el-select
          @change="storeChange"
          v-model="queryParams.type"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="dict in typeList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value*1"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="服务Key" prop="serviceKey">
        <el-select
          @change="storeChange"
          v-model="queryParams.serviceKey"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="dict in dict.type.service_market_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value*1"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="sysServiceMarketList">
      <el-table-column label="服务类型" align="center" prop="type">
        <template slot-scope="scope">
          {{ scope.row.type == '0' ? 'ERP服务' : scope.row.type == '1' ? '增值服务' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="服务Key" align="center" prop="serviceKey">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_market_type" :value="scope.row.serviceKey"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="serviceName"/>
      <el-table-column label="订单编号" align="center" prop="orderNumber"/>
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          {{
            scope.row.payStatus == '0' ? '待支付' : scope.row.payStatus == '1' ? '支付成功' : scope.row.payStatus == '-1' ? '支付失败' : scope.row.payStatus == '2' ? '待开户' : scope.row.payStatus == '3' ? '待审核' : scope.row.payStatus == '4' ? '审核通过' : scope.row.payStatus == '5' ? '审核不通过' : ''
          }}
        </template>
      </el-table-column>
      <el-table-column label="是否生效" align="center" prop="isEffect">
        <template slot-scope="scope">
          {{ scope.row.isEffect == '0' ? '否' : scope.row.isEffect == '1' ? '是' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.payStatus == '0'"
            size="mini"
            type="text"
            :icon="orderNumber == scope.row.orderNumber ? 'el-icon-loading' : 'el-icon-thumb'"
            @click="handlePayment(scope.row)"
          >支付
          </el-button>
          <el-button
            v-if="scope.row.payStatus == '2'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOpen(scope.row)"
          >开户
          </el-button>
          <el-button
            v-if="scope.row.serviceKey == '5' && scope.row.payStatus == '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSelect(scope.row)"
          >选择品牌
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务市场对话框 -->
    <el-dialog title="绑定品牌" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="brandForm" :model="brandParams" :rules="rules" label-width="100px">
        <el-form-item label="选择品牌" prop="brandId">
          <el-select v-model="brandParams.brandId" placeholder="请选择" clearable>
            <el-option
              v-for="dict in brandList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="支付" :visible.sync="paymentOpen" width="600px" append-to-body center>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">我已支付</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {buyRecord, storeBrand, chooseStoreBrand, pay} from "@/api/distribution";
import {queryUserShopList} from "@/api/shop/index";
import ImageUpload from '@/components/ImageUpload';
import store from '@/store';

export default {
  name: "recordPage",
  dicts: ['service_market_type'],
  components: {
    ImageUpload
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      total: 0,
      // 显示搜索条件
      showSearch: true,
      // 服务市场表格数据
      sysServiceMarketList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceKey: undefined,
        type: undefined,
        userId: store.getters.userId,
        storeId: undefined
      },
      // 表单校验
      rules: {
        brandParams: [
          {required: true, message: "请选择品牌", trigger: ["blur", "change"]}
        ]
      },
      typeList: [
        {
          value: 0,
          label: 'ERP服务'
        },
        {
          value: 1,
          label: '增值服务'
        }
      ],
      // 店铺列表
      storeList: [],
      // 品牌列表
      brandList: [],
      brandParams: {
        brandId: undefined,
        storeId: undefined,
        orderNumber: undefined
      },
      // 支付弹层
      paymentOpen: false,
      orderNumber: undefined
    };
  },
  created() {
    this.getEmpowerList();
    this.getList();
  },
  methods: {
    storeChange() {
      this.getList();
    },
    // 获取店铺账号选择列表
    getEmpowerList() {
      store.dispatch('GetShopList').then(res => {
        this.storeList = res.data;
      })
    },
    /** 查询服务市场列表 */
    getList() {
      this.loading = true;
      buyRecord(this.queryParams).then(response => {
        this.sysServiceMarketList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.brandParams = {
        brandId: undefined,
        storeId: undefined,
        orderNumber: undefined
      };
      this.resetForm("brandForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 开户
    handleOpen(row) {
      this.$router.push(`/distribution1/personAccount/` + row.orderNumber);
    },
    // 选择品牌
    handleSelect(row) {
      this.reset();
      this.open = true;
      storeBrand(row.storeId).then(res => {
        this.brandList = res.data;
      });
    },
    // 绑定品牌
    submitForm() {
      this.$refs["brandForm"].validate(valid => {
        if (valid) {
          chooseStoreBrand(this.brandParams).then(res => {
            this.$modal.msgSuccess("绑定成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    // 支付
    handlePayment(row) {
      this.orderNumber = row.orderNumber;
      pay({orderNumber: row.orderNumber}).then(res => {
        this.$confirm('', '支付', {
          confirmButtonText: '我已支付',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.getList();
        }).catch(() => {

        });
        const div = document.createElement('div'); // 创建div
        div.innerHTML = res.data; // 将返回的form 放入div
        document.body.appendChild(div);
        let len = document.forms.length - 1;
        document.forms[len].setAttribute('target', '_blank'); // 新开窗口跳转
        document.forms[len].submit();
        this.orderNumber = undefined;
      })
    }
  }
};
</script>
